let MS = require('./ms')
const request = require('./request')
const Error = require('./error')
const storage = require("./storage.js")
const {
    isString,
    isUndefined,
    isNumber,
    isBoolean,
    isObject
} = require('./dataType')

const easemob = class easemob {
    constructor() {

    }

    getEasemobConfig() {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EASEMOB.GET_EASEMOB_CONFIG.url,
                MS._config.parameters.EASEMOB.GET_EASEMOB_CONFIG.method, {}).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    save(options) {
        let params = {}
        if(!isUndefined(options) && isObject(options)) {
            if(!isUndefined(options.orgName) && isString(options.orgName)) {
                params.orgName = options.orgName;
            }
            if(!isUndefined(options.appName) && isString(options.appName)) {
                params.appName = options.appName;
            }
            if(!isUndefined(options.exist) && isBoolean(options.exist)) {
                params.exist = options.exist;
            }
            if(!isUndefined(options.clientId) && isString(options.clientId)) {
                params.clientId = options.clientId;
            }
            if(!isUndefined(options.clientSecret) && isString(options.clientSecret)) {
                params.clientSecret = options.clientSecret;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EASEMOB.SAVE.url,
                MS._config.parameters.EASEMOB.SAVE.method, params).then(res => {
                    if(res.success) {
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
            }).catch(err => {
                reject(err)
            })
        })
    }

    findChatMessageRecord(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if(!isUndefined(options) && isObject(options)) {
            if(!isUndefined(options.exist) && isBoolean(options.exist)) {
                params.exist = options.exist;
            }
            if(!isUndefined(options.startDate) && isString(options.startDate)) {
                params.startDate = options.startDate;
            }
            if(!isUndefined(options.endDate) && isString(options.endDate)) {
                params.endDate = options.endDate;
            }
            if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum;
            }
            if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EASEMOB.FIND_CHAT_MESSAGE_RECORD.url,
                MS._config.parameters.EASEMOB.FIND_CHAT_MESSAGE_RECORD.method, params).then(res => {
                    if(res.success) {
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
            }).catch(err => {
                reject(err)
            })
        })
    }

    getAccessToken() {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EASEMOB.GET_ACCESS_TOKEN.url,
                MS._config.parameters.EASEMOB.GET_ACCESS_TOKEN.method).then(res => {
                    if(res.success) {
                        storage.save('HX-ACCESS-TOKEN', res.data)
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
            }).catch(err => {
                reject(err)
            })
        })
    }

    currentAccessToken() {
        const data = storage.fetch('HX-ACCESS-TOKEN')
        if (data != undefined && data != null) {
            return typeof data === 'object' ? data : JSON.parse(data)
        } else {
            return null;
        }
    }
}

module.exports = easemob